﻿using log4net;
using Spring.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using WebService.WebCommon;

namespace WebService
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication :SpringMvcApplication
    {
        protected void Application_Start()
        {
            //Log4Net配置信息读取
            log4net.Config.XmlConfigurator.Configure();

            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            //从线程池取出一个线程来不停的扫描队列中的异常信息
            ThreadPool.QueueUserWorkItem((a)=>{
                while(true)
                {
                    if(AnomalyCapture.ErrorQueue.Count>0)
                    {
                        //异常信息出队
                        Exception ex = AnomalyCapture.ErrorQueue.Dequeue();
                        if(ex!=null)
                        {
                            ILog logger = LogManager.GetLogger("ErrorMsg");
                            logger.Error(ex.ToString());
                        }
                        else
                        {
                            //如果异常信息为空，则休息
                            Thread.Sleep(3000);
                        }
                    }
                    else
                    {
                        //一天扫描一次队列中的所有异常信息
                        Thread.Sleep(86400);
                    }
                }

        });    
        }
    }
}